package com.hjl.util;

import com.hjl.entity.Pojo;
import com.hjl.service.pojo.PojoService;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/PojoServlet"},name = "PojoServlet")
public class PoJoTestServlet extends HttpServlet {

    private Logger logger = Logger.getLogger(PoJoTestServlet.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");

        PojoService pojoService = (PojoService)context.getBean("pojoService");

        Pojo pojo = new Pojo();
        pojo.setUserName("赵");
        pojo.setUserRole(3);

        List<Pojo> pojoList = new ArrayList<Pojo>();
        pojoList = pojoService.findUsersWithConditions(pojo);

        for (Pojo pojoResult : pojoList) {
            logger.debug("testGetPojoList userCode："
                    + pojoResult.getUserCode() + " and userName："
                    + pojoResult.getUserName() + " and userRole："
                    + pojoResult.getUserRole() + " and userRoleName："
                    + pojoResult.getUserRoleName() + " and address："
                    + pojoResult.getAddress());
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        this.doGet(request, response);
    }
}
